import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {  
  
    private String url="jdbc:sqlserver://localhost:1433;databaseName=test";  
    private String userName="LiYan";  
    private String password="900425";  
    private Connection con;  
    public Main(){  
        try {  
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");  
            con=DriverManager.getConnection(url,userName,password);  
        } catch (ClassNotFoundException ex) {  
            System.out.println("Error:Can't Load the SQL Server Driver");  
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);  
        }catch(SQLException ex){  
            System.out.println("Error:Can't connect to Database!");  
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);  
        }  
  
    }  
    public static void main(String[] args) {  
       Main main=new Main();  
       String sql="select * from student";  
       if(main.con==null)  
           return;  
       try{  
           Statement st=main.con.createStatement();  
           ResultSet rs=st.executeQuery(sql);  
           while(rs.next()){  
               System.out.println(rs.getString("id")+","+rs.getString("name")+","
           +rs.getString("sex")+","+rs.getString("age"));  
           }  
       }catch(SQLException e){  
           e.printStackTrace();  
       }  
    }  
  
}  